package telepasea.app;

import java.util.StringTokenizer;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.net.Uri;
import android.util.Log;
import android.view.WindowManager;

public class TelepaSEA_Recv extends BroadcastReceiver {
	static String registration_id = null;
	static String c2dm_msg = "";

	@Override
	public void onReceive(Context context, Intent intent) {
		if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION"))
			handleRegistration(context, intent);
		else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) {
			
			c2dm_msg = intent.getExtras().getString("msg");
			Intent i = new Intent(context, PushAcitivty.class);
			i.putExtra("msg", c2dm_msg);
		    PendingIntent pi = PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_ONE_SHOT);
		
		    try {
				pi.send();
			} catch (CanceledException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			//msgParser(context, c2dm_msg);
		}
	}
	
	private void msgParser(final Context context, String c2dm_msg){
		StringTokenizer token = new StringTokenizer(c2dm_msg, "/");
		
		String name = token.nextToken();
		String sex = token.nextToken();
		String act = token.nextToken();
		final String phonenum = token.nextToken();
		
		System.out.println("Received Push Name/Sex/Act/PhoneNum======>" + name + "/" + sex + "/" + act + "/" + phonenum + "/");
		
		//Push Filtering
		Resources res = context.getResources();
		String[] itemlist = res.getStringArray(R.array.ActivityList);
		
		PushFilterUI myFilter = new PushFilterUI();
		
		String PF_ONOFF = Util.getSharedPreferencesValue(context, "telepasea", Context.MODE_PRIVATE, "PF_ONOFF");
		if (!PF_ONOFF.equals("checked"))
			return;
		
		if (sex.equals("1")) {
			String PF_M = Util.getSharedPreferencesValue(context, "telepasea", Context.MODE_PRIVATE, "PF_M");
			if (!PF_M.equals("checked"))
				return;
		}
		else if (sex.equals("2") ){
			String PF_F = Util.getSharedPreferencesValue(context, "telepasea", Context.MODE_PRIVATE, "PF_F");
			if (!PF_F.equals("checked"))
				return;
		}
		else
			return;
		
		int i;
		for (i = 0; i < 5; i++){
			if (act.equals(itemlist[i])){
				String PF_ACT = Util.getSharedPreferencesValue(context, "telepasea", Context.MODE_PRIVATE, "PF_ACT"+(i + 1));
				if (!PF_ACT.equals("checked"))
					return;
				break;
			}
		}
		
		if (i == 5)
			return;
		
		//Notify
		/*Toast toast = Toast.makeText(context, name + " : " + act +"\n",
				Toast.LENGTH_SHORT);
		
		toast.setGravity(Gravity.TOP | Gravity.CENTER, 0, 150);
		toast.show();*/
		
		Builder dlg = new AlertDialog.Builder(context);
		dlg.setTitle("");
		dlg.setMessage(name + " : " + act);
		
		dlg.setPositiveButton("CALL", new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialog, int which){
			    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phonenum));
			    context.startActivity(intent);
			}
		});
		
		dlg.setPositiveButton("SMS", new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialog, int which){
			    Intent intent = new Intent(Intent.ACTION_VIEW);
			    intent.putExtra("address", phonenum);
			    intent.setType("vnd.android-dir/mms-sms");
			    context.startActivity(intent);
			}
		});
		
		dlg.show();
	}

	private void handleRegistration(Context context, Intent intent) {
		registration_id = intent.getStringExtra("registration_id");
		System.out.println("registration_id====>" + registration_id);

		if (intent.getStringExtra("error") != null){
			Log.v("C2DM_REGISTRATION", ">>>>> Registration failed, should try again later." + "<<<<<");
			TelepaSEAActivity.guiHandler.sendEmptyMessageDelayed(2, 500);
		}else if (intent.getStringExtra("unregistered") != null){
			Log.v("C2DM_REGISTRATION", ">>>>> unregistration done, new messages from the authorized sender will be rejected <<<<<");
			TelepaSEAActivity.guiHandler.sendEmptyMessageDelayed(2, 500);
		}else if (registration_id != null) {
			System.out.println("registration_id complete!!");
			TelepaSEAActivity.guiHandler.sendEmptyMessageDelayed(1, 500);
		}
	}
}
